/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.spi.event.datamapping;

import com.inspur.edp.bff.api.exception.BffFrameworkException;
import com.inspur.edp.bff.api.exception.ErrorCodes;
import com.inspur.edp.bff.api.manager.IFSAction;
import com.inspur.edp.bff.api.manager.IFSManagerContext;
import com.inspur.edp.bff.api.manager.context.DataMapperContext;

import java.util.ArrayList;
import java.util.List;

public class BffDataMappingEventBroker {
    private static List<IBffDataMappingEventListener> eventLs = new ArrayList<IBffDataMappingEventListener>() {{
    }};

    private static IBffDataMappingEventListener getBffDataMappingEventListener(String className) {
        try {
            Class cls = Class.forName(className);
            IBffDataMappingEventListener listener = (IBffDataMappingEventListener) cls.newInstance();
            return listener;
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            throw new BffFrameworkException(e, ErrorCodes.BFF_FRAMEWORK_2020);
        }

    }

    public static final void fireBeforeTotalDataMapping(DataMapperContext mappingContext, IFSManagerContext bffcontext) {
        for (IBffDataMappingEventListener el : eventLs) {
            el.beforeTotalDataMapping(mappingContext, bffcontext);
        }
    }


    public static final void fireBeforeDataMapping(IFSAction datamapping, DataMapperContext mappingContext, IFSManagerContext bffcontext) {
        for (IBffDataMappingEventListener el : eventLs) {
            el.beforeDataMapping(datamapping, mappingContext, bffcontext);
        }
    }

    public static final void fireAfterDataMapping(DataMapperContext mappingContext, IFSManagerContext bffcontext) {
        for (IBffDataMappingEventListener el : eventLs) {
            el.afterDataMapping(mappingContext, bffcontext);
        }
    }

    public static final void fireAfterTotalDataMapping(DataMapperContext mappingContext, IFSManagerContext bffcontext, int TotalCount) {
        for (IBffDataMappingEventListener el : eventLs) {
            el.afterTotalDataMapping(mappingContext, bffcontext, TotalCount);
        }
    }

    public static final void fireQueryUnnormalStop(Exception e) {
        for (IBffDataMappingEventListener el : eventLs) {
            el.queryUnnormalStop(e);
        }
    }


}
